using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// 弓弩控制
public class BowController : MonoBehaviour
{
    public float moveSpeed = 6;    //速度：每秒移动6个单位长度
    public float angularSpeed = 90;    //角速度：每秒旋转90度
    public float jumpForce = 200f;    //跳跃参数
    public float horizontalRotateSensitivity = 5;    //水平视角灵敏度
    public float verticalRotateSensitivity = 5;    //垂直视角灵敏度
    private float xRotation = 0f;         // x旋转角度
    public bool canshoot = false;        // 是否可以射箭
 
    void FixedUpdate()        
    {
        Move();
        View();
    }
 
    void Move()      // 移动
    {
        float v = Input.GetAxis("Vertical");
        float h = Input.GetAxis("Horizontal");
 
        transform.Translate(Vector3.forward * v * Time.deltaTime * moveSpeed);
        transform.Translate(Vector3.right * h * Time.deltaTime * moveSpeed);
    }
 
    void SetCursorToCentre()   // 锁定鼠标到屏幕中心
    {
        //锁定鼠标后再解锁，鼠标将自动回到屏幕中心
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.lockState = CursorLockMode.None;
        //隐藏鼠标
        Cursor.visible = false;
    }
 
    void View()    // 控制视角
    {
        SetCursorToCentre();        //锁定鼠标到屏幕中心
        
        float mouseX = Input.GetAxis("Mouse X") * Time.deltaTime * angularSpeed* horizontalRotateSensitivity;
        transform.Rotate(Vector3.up * mouseX);     // "人"水平旋转
 
        float mouseY = Input.GetAxis("Mouse Y") * Time.deltaTime * angularSpeed * verticalRotateSensitivity;
        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -45f, 45f);          // 限制上下视角
        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);     // "人"不动，弓上下移动
    }
}
